import string
from util import capitalize_first_letter

def generate_file_from_template(type, outputs_folder, **kwargs):
    match type:
        case 'xml':
            type_suffix = 'Xml'
        case _:
            type_suffix = capitalize_first_letter(type)
    template_path = f'./core/templates/Template{type_suffix}.template'

    match type:
        case 'pojo':
            extension = 'java'
            type_suffix = ''
        case 'xml':
            extension = 'xml'
            type_suffix = 'Mapper'
        case _:
            extension = 'java'
            type_suffix = capitalize_first_letter(type)
    match type:
        case 'request' | 'result':
            output_path = f'{outputs_folder}/{kwargs["lA"]}/{kwargs["cM"]}{kwargs["cA"]}{type_suffix}.{extension}'
        case _:
            output_path = f'{outputs_folder}/{kwargs["lM"]}/{kwargs["cM"]}{type_suffix}.{extension}'

    with open(template_path, 'r') as file:
        template_content = file.read()

    template = string.Template(template_content)
    rendered_content = template.substitute(**kwargs)

    with open(output_path, 'w') as file:
        file.write(rendered_content)
